<?php
/**
 * 定制UpdAgentServer
 *
 * @author		黑冰 <954650386@qq.com>
 */

use \waf\Waf;
use \waf\log\WLog;
use \waf\server\UdpServer;
use \waf\modules\WCFunc;

class UdpAgentServer extends UdpServer
{
    public function onWorkerStart($objSServer, $workerId)
    {
        WLog::logSystem("onWorkerStart", __FILE__, __LINE__, __FUNCTION__);
        parent::onWorkerStart($objSServer, $workerId);

        // 定时1分钟落地数据
        swoole_timer_tick(60000, [$this, 'saveInterfaceData'],  $workerId);  // 接口调用
        swoole_timer_tick(60000, [$this, 'saveCountData'],      $workerId);  // 次数调用
    }

    public function onWorkerStop($objSServer, $workerId)
    {
        WLog::logSystem("onWorkerStop", __FILE__, __LINE__, __FUNCTION__);
        parent::onWorkerStop($objSServer, $workerId);

        // 落地数据
        $this->saveInterfaceData();
        $this->saveCountData();
    }

    /**
     * 接口调用数据统计落地
     *
     * @param int $timer_id
     * @param int $workerId
     */
    public function saveInterfaceData($timer_id = 0, $workerId = 0)
    {
        WLog::logInfo("in. workerId = $workerId, timer_id = $timer_id", __FILE__, __LINE__, __FUNCTION__);
        WLog::logDebug("data = ".WLog::toJson(Waf::WData()->getAllData()), __FILE__, __LINE__, __FUNCTION__);

        // 从当前进程中，获取累计的数据，存储到db
        // 一级key =>['waf_interface_statistics']
        // 二级key =>['server_name|module_name|interface_name']['sct' ,'sc', 'fct', 'fc']
        // sct:suc_cost_time , sc:suc_count, fct:fail_cost_time, fc:fail_count
        $key = Waf::getConfig('key', 'waf_interface_statistics');
        $arrReportData = Waf::WData()->getData($key);
        Waf::WData()->delData($key);

        if($arrReportData && is_array($arrReportData))
        {
            $time       = time();
            $log_time   = date('Y-m-d H:i:s', $time);
            $log_date   = date('Ymd', $time);

            foreach($arrReportData as $k => $v)
            {
                $arrT = array();
                $arrT = explode('|', $k);
                $server_name    = $arrT[0];
                $module_name    = $arrT[1];
                $interface_name = $arrT[2];
                unset($arrT);

                // check path
                $file_path  = Waf::getConfig('monitor', 'log_path').Waf::getConfig('monitor', 'interface_log_path').'/'.$server_name.'/'.$module_name.'/'.$interface_name;
                WLog::Directory($file_path);

                $file_name  = $file_path.'/'.$log_date.'.log';
                WLog::logDebug("file_name = $file_name", __FILE__, __LINE__, __FUNCTION__);

                $suc_cost_time  = intval($v['sct']);
                $suc_count      = intval($v['sc']);
                $fail_cost_time = intval($v['fct']);
                $fail_count     = intval($v['fc']);

                $strContent = "{$log_time}|{$suc_count}|{$suc_cost_time}|{$fail_count}|{$fail_cost_time}";
                file_put_contents($file_name,$strContent."\n", FILE_APPEND | LOCK_EX);
            }
        }
    }

    /**
     * 次数累计调用统计落地
     *
     * @param int $timer_id
     * @param int $workerId
     */
    public function saveCountData($timer_id = 0, $workerId = 0)
    {
        WLog::logInfo("in. workerId = $workerId, timer_id = $timer_id", __FILE__, __LINE__, __FUNCTION__);
        WLog::logDebug("data = ".WLog::toJson(Waf::WData()->getAllData()), __FILE__, __LINE__, __FUNCTION__);

        // 从当前进程中，获取累计的数据，存储到db
        // 一级key =>['waf_interface_statistics']
        // 二级key =>['server_name|module_name|interface_name']['sct' ,'sc', 'fct', 'fc']
        // sct:suc_cost_time , sc:suc_count, fct:fail_cost_time, fc:fail_count
        $key = Waf::getConfig('key', 'waf_count_statistics');
        $arrReportData = Waf::WData()->getData($key);
        Waf::WData()->delData($key);

        if($arrReportData && is_array($arrReportData))
        {
            $time       = time();
            $log_time   = date('Y-m-d H:i:s', $time);
            $log_date   = date('Ymd', $time);

            foreach($arrReportData as $k => $v)
            {
                $arrT = array();
                $arrT = explode('|', $k);
                $server_name    = $arrT[0];
                $module_name    = $arrT[1];
                $interface_name = $arrT[2];
                unset($arrT);

                $count     = floatval($v);

                // check path
                $file_path  = Waf::getConfig('monitor', 'log_path').Waf::getConfig('monitor', 'count_log_path').'/'.$server_name.'/'.$module_name.'/'.$interface_name;
                WLog::Directory($file_path);

                $file_name  = $file_path.'/'.$log_date.'.log';
                WLog::logDebug("file_name = $file_name", __FILE__, __LINE__, __FUNCTION__);

                $strContent = "{$log_time}|{$count}";
                file_put_contents($file_name,$strContent."\n", FILE_APPEND | LOCK_EX);
            }
        }
    }
}
?>